<?php	
	require "../common/db.inc";
	$conn = new mysqli($host, $username, $password, $dbname);	
	$conn->query("set names 'utf8'");		
	//check connection
	if (mysqli_connect_errno()) {
		printf("Connect failed: %s\n", mysqli_connect_error());
		exit();
	}

	$idCheck = $_REQUEST["id"];	
	
	$mana =  $_REQUEST["mana"];
		
	$sql = "call sp_GetUncheckedEbookById($idCheck);";
	$result = $conn->query($sql);
	
	if(mysqli_num_rows($result))
	{
		$row = $result->fetch_row();
		$id	= $row[0];
		$title = $row[1];
		$fileupload = $row[2];		
		$type = $row[3];
		$description = $row[4];
		$author = $row[5];
		$publisher = $row[6];
		$pagenum = $row[7];
		$img = $row[8];
		$size = $row[9];
		$category = $row[10];
		$updatedate = $row[11];
		$userid = $row[12]; 
		
		//insert vào bảng Ebook;
		$conn = new mysqli($host, $username, $password, $dbname);	
		$conn->query("set names 'utf8'");		
		//check connection
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		
		$sql = "call sp_AddEbook('$title', '$fileupload', '$type', '$description', '$author', '$publisher', '$pagenum', '$img', '$size', '$category', '$mana')";
		
		$objInsertResult =  $conn->query($sql);
		if(!$objInsertResult)
		{
			// có lỗi
			echo "có lỗi";
		}
		
		$objInsertRow = $objInsertResult->fetch_object();		
		$lastbook = $objInsertRow->intRecordKey;
		echo $lastbook;
		
		//insert vào bảng Ebook;
		$conn = new mysqli($host, $username, $password, $dbname);	
		$conn->query("set names 'utf8'");		
		//check connection
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		
		$sql = "call sp_AddFileTransfer('$userid', '$lastbook', 1)";
		if(!$conn->query($sql))
		{
			///có lỗi
			
		}
		
		//delete ebook đã duyệt
		$conn = new mysqli($host, $username, $password, $dbname);	
		$conn->query("set names 'utf8'");		
		//check connection
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
			
		$sql = "call sp_DeleteUncheckedEbook('$idCheck')";
		if(!$conn->query($sql))
		{
			///có lỗi
		}
		
		header("Location: home.php");
	}
?>